var path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
var package = require("./package.json");
module.exports = {
    entry:{
        bundle:'./src/router/app.js',
        vendor: ['react', 'react-dom', 'jquery','react-router']
    },
    output: {
        path: __dirname+ '/build',
        // filename: "[name].[chunkhash:5].js",
        // filename: "[name].js",
        // publicPath:'/build/',
        // chunkFilename: '[name].chunk.js',
        filename: package.version+'.[name].[chunkhash:5].js', //编译后的文件名字
        chunkFilename: '[name].[chunkhash:5].js'
    },
    module: {
        rules: [
            // {
            //     test: /\.css$/, use: 'style-loader!css-loader'
            // },
            {
                use: 'babel-loader',
                exclude: /node_modules/,
                test: /\.js[x]?$/
            },
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                  fallback: 'style-loader',
                  //resolve-url-loader may be chained before sass-loader if necessary
                  use: ['css-loader', 'postcss-loader','sass-loader']
                }),
                exclude: /^node_modules$/
            },{
                test: /\.less$/,
                use: ExtractTextPlugin.extract({
                  fallback: 'style-loader',
                  //resolve-url-loader may be chained before sass-loader if necessary
                  use: ['css-loader','less-loader']
                }),
                exclude: /^node_modules$/
            },  
            {
                test: /\.css$/,
                use: ['style-loader','css-loader','postcss-loader'],
                exclude: /^node_modules$/
            },
            //file-loader默认编译带了hash值这里是让他用原来的名字
            { 
                test: /\.(png|jpg|gif)$/, use: "file-loader?name=[name].[ext]" 
            },
            {test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/, loader: "file-loader" }
        ]
    },
    // devtool: 'eval-source-map',
    // resolve: {
    //     extensions: ['*','.js', '.jsx'] //后缀名自动补全
    //     // alias: {
    //     //     'Component': __dirname + '/src/scripts/comp-common/index.js',
    //     //     'actions': '../actions/index.js',
    //     //     'Header': './Header.js'
    //     // }
    // }
    // ,
    plugins: [
        new HtmlWebpackPlugin({
          title:'工单系统',
          template:  __dirname + "/src/index.tmpl.html", // 源模板文件
          filename: './index.html', // 输出文件【注意：这里的根路径是module.exports.output.path】
          showErrors: true,
          inject: 'body',
          hash: true
        }),
        new webpack.optimize.CommonsChunkPlugin('vendor'),
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin(),
        new ExtractTextPlugin('style.css')
    ],
    resolve: {
        alias: {
            "ui": path.join(process.cwd(), "./src/components/ui"),
            "business": path.join(process.cwd(), "./src/components/business"),
            "actions":path.join(process.cwd(), "./src/actions"),
            "reducers":path.join(process.cwd(), "./src/reducers"),
            "styles": path.join(process.cwd(), "./src/styles"),
            "common": path.join(process.cwd(), "./src/common")
        }
    }
};
